var packageName
let DYList = []
let xhsList = []
let spList = []
let KSList = []
let SXHuaShu = []
function DouYin (){
    packageName = 'com.ss.android.ugc.aweme'
    DYList = global.WEB_PARAMS.plList
    // 打开视频App
    launch(packageName);
    toast('打开抖音')
    
    // App加载完成
    waitForPackage(packageName);
    toast('加载完成')
    sleep(8000)
    
    // 点击消息
    if(text("以后再说").className("android.widget.TextView").depth("7").drawingOrder("1").findOne(1000)){
        text("以后再说").className("android.widget.TextView").depth("7").drawingOrder("1").click() 
    }
    const xx = className("android.widget.TextView").depth("19").drawingOrder("1").text("消息").findOne(1000)
    if(xx){
        log('找到消息')
        click(xx.bounds().centerX(),xx.bounds().centerY())
    }else{
        toast('没找到消息');
        console.log('没找到消息');
        back()
    }
    sleep(random(1000,3000))
    backDouYin()
}   

function getSX(){
    const res = http.get(`https://www.diandianyouke.cn:8081/tuokecomment/tuoke_comment/selectSXHSByStoreId/${global.WEB_PARAMS.storeId}`)
    SXHuaShu = res.body.json().data
    console.log(random(0,SXHuaShu.length-1));
}


function backDouYin(){
    className("android.widget.Button").desc("搜索").depth("18").drawingOrder("1").findOne(1000).click()
    sleep(random(1000,3000))
    console.log(DYList);
    DYList.forEach(e=>{
        console.log(e);
        className("android.widget.EditText").depth("6").drawingOrder("3").text("搜索").findOne().setText(e.remarkDYH.split('：')[1])
        sleep(random(1000,3000))
        const serch = text("视频、用户、音乐、话题、地点等").drawingOrder("5").findOne(3000)
        if(serch){
            toast('找到搜索')
        }
        sleep(random(1000,3000))
        click(serch.bounds().centerX(), serch.bounds().centerY())
        sleep(random(3000,5000))
        const user = className("android.widget.Button").text("用户").depth("16").findOne(1000)
        click(user.bounds().centerX(), user.bounds().centerY())
        sleep(random(1000,2000))
        const userHao = className("com.lynx.tasm.behavior.ui.LynxFlattenUI").depth("19").drawingOrder("1").findOne(2000)
        if(userHao){
            toastLog('找到用户')
            // console.log(userHao.child(0).text());
            click(userHao.bounds().centerX(), userHao.bounds().centerY())
            sleep(random(2000,3000))
            huiFuDouYin(e)
        }else{
            toastLog('没有找到用户')
        }
    })
}

function huiFuDouYin(e){
    sleep(random(1000,2000))
    className('android.widget.ImageView').depth(8).desc('更多').drawingOrder(8).findOne(2000).click()
    sleep(random(2000,3000))
    const faSXDouYin = className('android.widget.TextView').text('发私信').findOne(2000)
    click(faSXDouYin.bounds().centerX(), faSXDouYin.bounds().centerY())
    sleep(random(3000,5000))
    let input = className("android.widget.EditText").depth("16").findOne(2000)
    if(!input){
        toastLog('没有找到输入框')
        input = className("android.widget.EditText").depth("16").findOne(2000)
    }
    input.setText(SXHuaShu[random(0,SXHuaShu.length-1)].sxhs)
    sleep(random(2000,5000))
    className("android.widget.ImageView").desc("发送").depth("16").click()
    http.postJson('https://www.diandianyouke.cn:8081/tuokecomment/tuoke_comment/updateRemark',e)
    back()
    sleep(random(2000,3000))
    back()
    sleep(random(2000,3000))
    back()
}

getSX()
DouYin()